package com.xcity.game.activity.str;

import java.util.Collection;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import com.xcity.Server;
import com.xcity.db.entity.Player;
import com.xcity.game.activity.ActivityService;
import com.xcity.game.activity2.ActivityIds;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.player.PlayerService;
import com.xcity.pomelo.push.msg.ActivityStatePush;

import naga.x.App;
import naga.x.game.time.TimeUpdater;
import naga.x.net.Packet;

public class StrengthMeal implements Runnable {

	protected byte type;
	protected long time; // 奖励时间
	protected long timeout; // 过期时间（3小时）
	protected byte state; // @see STATE_INIT/STATE_OPENED/STATE_CLOSED
	protected ScheduledFuture<?> future;
	
	// 类型：午餐、晚餐
	public static final byte TYPE_LUNCH = 0, TYPE_DINNER = 1;
	// 时间状态：初始化、开放、关闭
	public static final byte STATE_INIT = 0, STATE_OPENED = 1, STATE_CLOSED = 2;
	// 奖励状态：不能领取，可领取，已领取
	public static final byte CAN_NOT_RECEIVE = -1, CAN_RECEIVE = 0, RECEIVED = 1;
	
	public StrengthMeal(byte type) {
		this.type = type;
	}
	
	public void reset(long time) {
		if (future != null && !future.isDone() && !future.isCancelled()) {
			future.cancel(false);
			future = null;
		}
		this.time = time;
		this.timeout = time + 3 * 60 * 60 * 1000;
		this.state = STATE_INIT;
		long delay = time - TimeUpdater.getInstance().now();
		if (delay < 0 && this.timeout > TimeUpdater.getInstance().now()) { // 只要没过期就开启
			delay = 0;
		}
		if (delay >= 0) {
			future = App.getApp().getSchedule().schedule(this, delay, TimeUnit.MILLISECONDS);
		} else {
			this.state = STATE_CLOSED;
		}
	}
	
	public void close() {
		if (state != STATE_CLOSED) {
			state = STATE_CLOSED;
			if (future != null && !future.isDone() && !future.isCancelled()) {
				future.cancel(false);
				future = null;
			}
			fireClosed();
		}
	}
	
	public long getTime() {
		return time;
	}
	
	public byte getState() {
		return state;
	}
	
	@Override
	public void run() {
		if (state == STATE_INIT) {
			state = STATE_OPENED;
//			Server.broadcast(new ActivityStatePush(ActivityIds.ACTIVITY_STRENGTH_MEAL, ActivityService.STATE_OPENED));
			fireOpened();
			long delay = timeout - TimeUpdater.getInstance().now();
			future = App.getApp().getSchedule().schedule(this, delay, TimeUnit.MILLISECONDS); // 计划关闭
		} else if (state == STATE_OPENED) {
			state = STATE_CLOSED;
			fireClosed();
		}
	}
	
	private static void fireOpened() {
		Packet packet = new ActivityStatePush(ActivityIds.ACTIVITY_STRENGTH_MEAL, ActivityService.STATE_OPENED);
		Collection<Player> players = App.getApp().getServiceManager().get(PlayerService.class).getOnlinePlayers();
		for (Player player : players) {
			if (player.getLevel() >= GameStaticConfig.strengthMealActivityOpenLvl) {
				player.send(packet);
			}
		}
	}
	
	private static void fireClosed() {
		Server.broadcast(new ActivityStatePush(ActivityIds.ACTIVITY_STRENGTH_MEAL, ActivityService.STATE_CLOSED));
	}
	
}
